package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.config.JwtTemplate;
import com.demo.controller.from.LoginFrom;
import com.demo.exetion.MainExection;
import com.demo.model.MovieUser;
import com.demo.dao.MovieUserDao;
import com.demo.service.MovieUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-04-13
 */
@Service
public class MovieUserServiceImpl extends ServiceImpl<MovieUserDao, MovieUser> implements MovieUserService {

    @Override
    public String login(LoginFrom loginFrom) throws MainExection {
        QueryWrapper<MovieUser> movieUserQueryWrapper=new QueryWrapper<>();
        movieUserQueryWrapper.eq("user_username",loginFrom.getUserUsername()).eq("user_password",loginFrom.getUserPassword())
                .eq("user_start",0);
        MovieUser movieUser = baseMapper.selectOne(movieUserQueryWrapper);
        if (movieUser==null)throw new MainExection(500,"账号或密码不正确");
        JwtTemplate jwtTemplate=new JwtTemplate();
        Map<String,Object> maps=new HashMap<>();
        maps.put("user_id",movieUser.getUserId());
        maps.put("user_name",movieUser.getUserName());
        String jwt = jwtTemplate.createJwt(60,maps);
        return jwt;
    }
}
